/**
 * base action,it packages some base function
 * @author lixin
 */
package cn.edu.bit.lixin.blog.action;

import java.util.Map;

import org.apache.struts2.ServletActionContext;

import cn.edu.bit.lixin.blog.dao.BaseDAO;
import cn.edu.bit.lixin.blog.entity.User;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public abstract class BaseActionSupport extends ActionSupport {
	private String message;//返回的消息
	private String title;//页面显示的标题
	private BaseDAO dao;
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public BaseDAO getDao() {
		return dao;
	}
	public void setDao(BaseDAO dao) {
		this.dao = dao;
	}
	
	/**
	 * reset message and title of page
	 */
	public void resetMessages(){
		this.setMessage("");
		this.setTitle("");
	}
	
	/**
	 * get the value in the session
	 * @param name
	 * @return object
	 */
	public static Object getSession(String name){
		ActionContext actionContext = ActionContext.getContext();
		Map session = actionContext.getSession();
		return session.get(name);
	}
	
	/**
	 * put the value into the session
	 * @param name
	 * @param value
	 */
	public static void setSession(String name, Object value){
		ActionContext actionContext = ActionContext.getContext();
		Map session = actionContext.getSession();
		session.put(name, value);
	}
	
	/**
	 * get sessioned user
	 * @return
	 */
	public User getSessionLoginedUser(){
		return (User)getSession("loginedUser");
	}
	
	/**
	 * get the value of parameter in the request
	 * @param name
	 * @return string
	 */
	public String getParameter(String name){
		return ServletActionContext.getRequest().getParameter(name);
	}
	
	/**
	 * get the value of parameter in the request,the return type is int
	 * @param name
	 * @return int
	 */
	public int getParameterInt(String name){
		String result = getParameter(name);
		
		if (result == null){
			return 0;
		}else{
			try {
				return Integer.parseInt(result);
			}catch(NumberFormatException e){
				e.printStackTrace();
			}
		}
		return 0;
	}
	
	/**
	 * set attribute of request
	 * @param name
	 * @param value
	 */
	public void setAttribute(String name, Object value){
		ServletActionContext.getRequest().setAttribute(name, value);
	}
	
	/**
	 * get attribute of request
	 * @param name
	 * @return object
	 */
	public Object getAttribute(String name){
		return ServletActionContext.getRequest().getAttribute(name);
	}
}











